# coding: utf-8
# author: t_wolf

from settings import *

# 判断导入CONFIG为框架
URL_MAP = entrance_init(APP_CONFIGS)

logger.debug(
    "Application: [%s], Current url_mode: [%s], listening URL_MAP: %s" % (
        APP_CONFIGS.APP.app_name, APP_CONFIGS.APP.url_mode, URL_MAP
    )
)

# 创建Application
app = web.application(URL_MAP, globals())
inject_object_by_dict(app, {
    'app_name': APP_CONFIGS.APP.app_name,
    'app_path': APP_CONFIGS.APP.app_path
})

# 会话传送
if APP_CONFIGS.APP.session_enabled and web.ctx.get('session') is None:
    SESSION.session_update(APP_CONFIGS.APP.session_options)
    session = SESSION(app).init()
    app.add_processor(
        web.loadhook(lambda: SESSION.transfer(session))
    )

# 注册后台任务
EasyWeb.reg_background_worker(
    APP_CONFIGS.APP.app_name,
    scan_back_worker(APP_CONFIGS.APP.app_path)
)

# 定义UWSGI的Application
application = app.wsgifunc()

# 利用web.py自身WSGI启动Application
if __name__ == '__main__':
    app.run()
